From a6974964630f07bfb4742103d39ecb2d968691af Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sun, 23 Apr 2023 14:17:25 -0600 Subject: [PATCH] struct tm -> std::tm, memset of tm goes to {} (#1079) * struct tm -> std::tm, memset of tm goes to {}. * init std::tm related variables. tidy found found the following warnigns related to our usage of std::tm: cppcoreguidelines-pro-type-member-init and std::tm*: cppcoreguidelines-init-variables * make GPS_D600_Get var tm automatic storage duration and intialize it. --- brauniger_iq.cc | 2 +- defs.h | 4 ++-- garmin_txt.cc | 2 +- gdb.cc | 6 ++---- globalsat_sport.cc | 4 ++-- jeeps/gpsapp.cc | 8 +++----- navilink.cc | 8 ++------ sbn.cc | 2 +- trackfilter.cc | 4 ++-- unicsv.cc | 13 +++++-------- util.cc | 20 ++++++++++---------- wbt-200.cc | 2 +- xcsv.cc | 11 +++++------ 13 files changed, 37 insertions(+), 49 deletions(-) diff --git a/brauniger_iq.cc b/brauniger_iq.cc index 371096779..672326356 100644 --- a/brauniger_iq.cc +++ b/brauniger_iq.cc @@ -75,7 +75,7 @@ static void rd_deinit() static int process_data(const unsigned char* data) { static int remaining = 100; - static struct tm tm; + static std::tm tm; static time_t start, creation; static route_head* track; static unsigned char interval; diff --git a/defs.h b/defs.h index 6fe4c28b1..b01100b7e 100644 --- a/defs.h +++ b/defs.h @@ -1024,8 +1024,8 @@ int str_match(const char* str, const char* match); int xvasprintf(char** strp, const char* fmt, va_list ap); char* strupper(char* src); char* strlower(char* src); -time_t mklocaltime(struct tm* t); -time_t mkgmtime(struct tm* t); +time_t mklocaltime(std::tm* time); +time_t mkgmtime(std::tm* time); bool gpsbabel_testmode(); gpsbabel::DateTime current_time(); QDateTime dotnet_time_to_qdatetime(long long dotnet); diff --git a/garmin_txt.cc b/garmin_txt.cc index 24d7e4dd3..415a48a81 100644 --- a/garmin_txt.cc +++ b/garmin_txt.cc @@ -365,7 +365,7 @@ print_position(const Waypoint* wpt) static void print_date_and_time(const time_t time, const int time_only) { - struct tm tm; + std::tm tm{}; char tbuf[32]; if (time < 0) { diff --git a/gdb.cc b/gdb.cc index dd4c2bc33..d8b864877 100644 --- a/gdb.cc +++ b/gdb.cc @@ -1108,7 +1108,6 @@ GdbFormat::write_header() const char buff[128], tbuff[32]; char* c; int len, n = 0; - struct tm tm; FWRITE("MsRc", 4); // Signature FWRITE_i16(0x66); // Primary File Format @@ -1142,8 +1141,7 @@ GdbFormat::write_header() const */ - memset(&tm, 0, sizeof(tm)); - + std::tm tm{}; n = sscanf(gdb_release_date+7, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec); if (n != 6) { // The $Date string in gdb_release_date[] above is bad. @@ -1155,7 +1153,7 @@ GdbFormat::write_header() const n = strftime(tbuff, sizeof(tbuff), "%b %d %Y*%H:%M:%S", &tm); if (n == 0) { - // The build of the struct tm was bad. + // The build of the std::tm was bad. fatal(MYNAME ": internal date generation error for %s\n", gdb_release_date + 7); } diff --git a/globalsat_sport.cc b/globalsat_sport.cc index 8cef720e8..813586728 100644 --- a/globalsat_sport.cc +++ b/globalsat_sport.cc @@ -520,8 +520,8 @@ GlobalsatSportFormat::track_read() } /* - * GPS year: 2000+; struct tm year: 1900+ - * GPS month: 1-12, struct tm month: 0-11 + * GPS year: 2000+; std::tm year: 1900+ + * GPS month: 1-12, std::tm month: 0-11 */ QDate gpsDate = QDate(header.dateStart.Year+2000, header.dateStart.Month, header.dateStart.Day); diff --git a/jeeps/gpsapp.cc b/jeeps/gpsapp.cc index 1d28ed0ef..0eb115939 100644 --- a/jeeps/gpsapp.cc +++ b/jeeps/gpsapp.cc @@ -5872,7 +5872,7 @@ int32 GPS_A600_Send(const char* port, time_t Time) time_t GPS_D600_Get(GPS_PPacket& packet) { UC* p; - static struct tm ts; + std::tm ts{}; p = packet.data; @@ -5901,12 +5901,10 @@ time_t GPS_D600_Get(GPS_PPacket& packet) void GPS_D600_Send(GPS_PPacket& packet, time_t Time) { UC data[10]; - UC* p; - struct tm* ts; - p = data; + UC* p = data; - ts = localtime(&Time); + std::tm* ts = localtime(&Time); *p++ = ts->tm_mon+1; *p++ = ts->tm_mday; diff --git a/navilink.cc b/navilink.cc index d08c50564..42da11b3a 100644 --- a/navilink.cc +++ b/navilink.cc @@ -366,9 +366,7 @@ decode_datetime(const unsigned char* buffer) static void encode_datetime(time_t datetime, unsigned char* buffer) { - struct tm* tm; - - if ((tm = gmtime(&datetime)) != nullptr) { + if (std::tm* tm = gmtime(&datetime); tm != nullptr) { buffer[0] = tm->tm_year - 100; buffer[1] = tm->tm_mon + 1; buffer[2] = tm->tm_mday; @@ -799,9 +797,7 @@ decode_sbp_datetime_packed(const unsigned char* buffer) * SSSSSSMM MMMMHHHH Hdddddmm mmmmmmmm */ - struct tm tm; - - memset(&tm, 0, sizeof(tm)); + std::tm tm{}; tm.tm_sec = buffer[0] & 0x3F; tm.tm_min = ((buffer[0] & 0xC0) >> 6) | ((buffer[1] & 0x0F) << 2); diff --git a/sbn.cc b/sbn.cc index 7ee6edcc0..f28669026 100644 --- a/sbn.cc +++ b/sbn.cc @@ -220,7 +220,7 @@ decode_sbn_mode(const unsigned char* mode) static void decode_sbn_datetime(const unsigned char* buffer, Waypoint* waypt) { - struct tm tm; + std::tm tm{}; int ms = be_readu16(buffer + 6); tm.tm_sec = ms / 1000; diff --git a/trackfilter.cc b/trackfilter.cc index b966a9355..cd870212c 100644 --- a/trackfilter.cc +++ b/trackfilter.cc @@ -252,7 +252,7 @@ void TrackFilter::trackfilter_split_init_rte_name(route_head* track, const gpsba // Uggh. strftime format exposed to user. time_t time = dt.toTime_t(); - struct tm tm = *gmtime(&time); + std::tm tm = *gmtime(&time); char buff[128]; strftime(buff, sizeof(buff), opt_title, &tm); track->rte_name = buff; @@ -279,7 +279,7 @@ void TrackFilter::trackfilter_pack_init_rte_name(route_head* track, const gpsbab dt = wpt->GetCreationTime(); } time_t t = dt.toTime_t(); - struct tm tm = *gmtime(&t); + std::tm tm = *gmtime(&t); char buff[128]; strftime(buff, sizeof(buff), opt_title, &tm); track->rte_name = buff; diff --git a/unicsv.cc b/unicsv.cc index 743ebbc31..03c3147c4 100644 --- a/unicsv.cc +++ b/unicsv.cc @@ -23,7 +23,7 @@ #include // for fabs, lround #include // for NULL, sscanf -#include // for memset, strchr, strncpy +#include // for strchr, strncpy #include // for gmtime #include // for QByteArray @@ -230,10 +230,9 @@ UnicsvFormat::unicsv_parse_date(const char* str, int* consumed) { int p1, p2, p3; char sep[2]; - struct tm tm; + std::tm tm{}; int lconsumed = 0; - memset(&tm, 0, sizeof(tm)); int ct = sscanf(str, "%d%1[-.//]%d%1[-.//]%d%n", &p1, sep, &p2, sep, &p3, &lconsumed); if (consumed && lconsumed) { *consumed = lconsumed; @@ -348,7 +347,7 @@ UnicsvFormat::unicsv_adjust_time(const time_t time, const time_t* date) const if (opt_utc) { res += xstrtoi(opt_utc, nullptr, 10) * SECONDS_PER_HOUR; } else { - struct tm tm = *gmtime(&res); + std::tm tm = *gmtime(&res); res = mklocaltime(&tm); } return QDateTime::fromSecsSinceEpoch(res, Qt::UTC); @@ -502,7 +501,7 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf) char is_localtime = 0; garmin_fs_t* gmsd; double d; - struct tm ymd; + std::tm ymd{}; int src_datum = unicsv_datum_idx; int ns = 1; int ew = 1; @@ -510,7 +509,6 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf) auto* wpt = new Waypoint; wpt->latitude = kUnicsvUnknown; wpt->longitude = kUnicsvUnknown; - memset(&ymd, 0, sizeof(ymd)); int column = -1; const QStringList values = csv_linesplit(ibuf, unicsv_fieldsep, "\"", 0, CsvQuoteMethod::rfc4180); @@ -967,8 +965,7 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf) time_t t = date + time; if (is_localtime) { - struct tm tm; - tm = *gmtime(&t); + std::tm tm = *gmtime(&t); if (opt_utc) { wpt->SetCreationTime(mkgmtime(&tm)); } else { diff --git a/util.cc b/util.cc index 448d02eb2..e67b0bcaa 100644 --- a/util.cc +++ b/util.cc @@ -568,13 +568,13 @@ le_write32(void* ptr, const unsigned value) */ time_t -mkgmtime(struct tm* t) +mkgmtime(std::tm* time) { static const int m_to_d[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; - short month = t->tm_mon; - short year = t->tm_year + month / 12 + 1900; + short month = time->tm_mon; + short year = time->tm_year + month / 12 + 1900; month %= 12; if (month < 0) { year -= 1; @@ -587,14 +587,14 @@ mkgmtime(struct tm* t) result += (year - 1968) / 4; result -= (year - 1900) / 100; result += (year - 1600) / 400; - result += t->tm_mday; + result += time->tm_mday; result -= 1; result *= 24; - result += t->tm_hour; + result += time->tm_hour; result *= 60; - result += t->tm_min; + result += time->tm_min; result *= 60; - result += t->tm_sec; + result += time->tm_sec; return (result); } @@ -603,16 +603,16 @@ mkgmtime(struct tm* t) * which is evaluated by mktime */ time_t -mklocaltime(struct tm* t) +mklocaltime(std::tm* time) { time_t result; - struct tm check = *t; + std::tm check = *time; check.tm_isdst = 0; result = mktime(&check); check = *localtime(&result); if (check.tm_isdst == 1) { /* DST is in effect */ - check = *t; + check = *time; check.tm_isdst = 1; result = mktime(&check); } diff --git a/wbt-200.cc b/wbt-200.cc index ccbc20fbf..d7b9c1333 100644 --- a/wbt-200.cc +++ b/wbt-200.cc @@ -521,7 +521,7 @@ std::array split_date(uint32_t tim) static time_t decode_date(uint32_t tim) { auto [sec, min, hour, mday, mon, year] = split_date(tim); - struct tm t; + std::tm t{}; t.tm_sec = sec; t.tm_min = min; diff --git a/xcsv.cc b/xcsv.cc index 717f55c83..2d42b6f6b 100644 --- a/xcsv.cc +++ b/xcsv.cc @@ -29,7 +29,7 @@ #include // for fabs, pow #include // for snprintf, sscanf #include // for strtod -#include // for strlen, strncmp, strcmp, memset +#include // for strlen, strncmp, strcmp #include // for gmtime, localtime, time_t, mktime, strftime #include // for optional @@ -263,8 +263,7 @@ XcsvFormat::yyyymmdd_to_time(const QString& s) time_t XcsvFormat::sscanftime(const char* s, const char* format, bool gmt) { - struct tm stm; - memset(&stm, 0, sizeof(stm)); + std::tm stm{}; if (strptime(s, format, &stm)) { if ((stm.tm_mday == 0) && (stm.tm_mon == 0) && (stm.tm_year == 0)) { @@ -312,7 +311,7 @@ XcsvFormat::addhms(const char* s, const char* format) QString XcsvFormat::writetime(const char* format, time_t t, bool gmt) { - static struct tm* stmp; + static const std::tm* stmp; if (gmt) { stmp = gmtime(&t); @@ -338,8 +337,8 @@ XcsvFormat::writetime(const char* format, const gpsbabel::DateTime& t, bool gmt) QString XcsvFormat::writehms(const char* format, time_t t, bool gmt) { - static struct tm no_time = tm(); - static struct tm* stmp = &no_time; + static const std::tm no_time{}; + static const std::tm* stmp = &no_time; if (gmt) { stmp = gmtime(&t); -- 2.30.2